#if UNITY_STANDALONE || UNITY_ANDROID || UNITY_IOS || UNITY_WSA || UNITY_WEBGL
using GameDesigner;
using UnityEngine;
#endif

namespace ActorSystem.Template
{
#if UNITY_STANDALONE || UNITY_ANDROID || UNITY_IOS || UNITY_WSA || UNITY_WEBGL
    public class Player : ActorBase
    {
        public AnimatorStateMachineView view;

        //public override void Awake()
        //{
        //    view.Init();
        //}

        private void OnEnable()
        {
            StateMachineSystem.AddStateController(view.controller);
        }

        private void OnDisable()
        {
            StateMachineSystem.AddStateController(view.controller);
        }
    }

    public class PlayerIdle : StateBehaviour
    {
        private ActorBase self;

        public override void OnInit()
        {
            self = transform.GetComponent<ActorBase>();
        }

        public override void OnUpdate()
        {
            var dir = InputEx.Direction;
            if (dir != Vector3.zero)
            {
                ChangeState(1);
            }
        }
    }

    public class PlayerRun : StateBehaviour
    {
        private ActorBase self;

        public override void OnInit()
        {
            self = transform.GetComponent<ActorBase>();
        }

        public override void OnUpdate()
        {
            var dir = Camera.main.transform.Transform3Dir();
            if (dir == Vector3.zero)
            {
                ChangeState(0);
            }
            else
            {
                transform.LerpRotation(dir);
                //transform.Translate(0, 0, self.Property.MoveSpeed * Time.deltaTime);
            }
        }
    }

    public class PlayerIdle2D : StateBehaviour
    {
        private ActorBase self;

        public override void OnInit()
        {
            self = transform.GetComponent<ActorBase>();
        }

        public override void OnUpdate()
        {
            var dir = InputEx.Direction2D;
            if (dir != Vector2.zero)
            {
                ChangeState(1);
            }
        }
    }

    public class PlayerRun2D : StateBehaviour
    {
        private ActorBase self;
        private Rigidbody2D rigidbody;

        public override void OnInit()
        {
            self = transform.GetComponent<ActorBase>();
            rigidbody = transform.GetComponent<Rigidbody2D>();
            rigidbody.gravityScale = 0; // 禁用重力
        }

        public override void OnUpdate()
        {
            var dir = InputEx.Direction2D;
            if (dir == Vector2.zero)
            {
                ChangeState(0);
#if UNITY_6000_0_OR_NEWER
                rigidbody.linearVelocity = Vector2.zero;
#else
                rigidbody.velocity = Vector2.zero;
#endif
            }
            else
            {
                Vector2 moveDirection = dir.normalized * 6f;//self.Property.MoveSpeed;
#if UNITY_6000_0_OR_NEWER
                rigidbody.linearVelocity = new Vector2(moveDirection.x, moveDirection.y);
#else
                rigidbody.velocity = new Vector2(moveDirection.x, moveDirection.y);
#endif
                if (dir.x > 0)
                    transform.rotation = Quaternion.Euler(0, 0, 0);
                else if (dir.x < 0)
                    transform.rotation = Quaternion.Euler(0, 180f, 0);
            }
        }
    }
#endif
}